package com.stox.core.dao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.stox.core.ui.swing.util.ThrowableReporter;
import com.stox.core.util.Constant;

public class FileTemplate {

	public String read(String path){
		BufferedReader bufferedReader = null;
		try{
			bufferedReader = new BufferedReader(new FileReader(path));
			String line = null;
			StringBuffer stringBuffer = new StringBuffer();
			while((line = bufferedReader.readLine()) != null){
				stringBuffer.append(line+Constant.lineSeparator);
			}
			bufferedReader.close();
			return stringBuffer.toString();
		}catch(Exception e){
			try {
				if(bufferedReader != null){
					bufferedReader.close();
				}
			} catch (IOException e1) {
				
			}
		}
		return null;
	}
	
	public String read(String path, int count){
		BufferedReader bufferedReader = null;
		try{
			bufferedReader = new BufferedReader(new FileReader(path));
			String line = null;
			StringBuffer stringBuffer = new StringBuffer();
			while((line = bufferedReader.readLine()) != null && count > 0){
				stringBuffer.append(line+Constant.lineSeparator);
				count--;
			}
			bufferedReader.close();
			return stringBuffer.toString();
		}catch(Exception e){
			try {
				if(bufferedReader != null){
					bufferedReader.close();
				}
			} catch (IOException e1) {
				
			}
		}
		return null;
	}
	
	public void write(Object object, String path){
		BufferedWriter bufferedWriter = null;
		try{
			File file = new File(path);
			if(!file.exists()){
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			bufferedWriter = new BufferedWriter(new FileWriter(file,true));
			if(object != null){
				bufferedWriter.append(object.toString()+Constant.lineSeparator);
			}
			bufferedWriter.flush();
			bufferedWriter.close();
		}catch(Exception e){
			try{
				if(bufferedWriter != null){
					bufferedWriter.flush();
					bufferedWriter.close();
				}
			}catch(Exception e1){
				ThrowableReporter.report(e);
			}
			ThrowableReporter.report(e);
		}
	}
	
	public void write(List<?> list, String path){
		BufferedWriter bufferedWriter = null;
		try{
			List<?> written = new ArrayList<Object>();
			File file = new File(path);
			if(!file.exists()){
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			bufferedWriter = new BufferedWriter(new FileWriter(file,true));
			for(Object object : list){
				if(object != null && !written.contains(object)){
					bufferedWriter.append(object.toString()+Constant.lineSeparator);
				}
			}
			bufferedWriter.flush();
			bufferedWriter.close();
		}catch(Exception e){
			try{
				if(bufferedWriter != null){
					bufferedWriter.flush();
					bufferedWriter.close();
				}
			}catch(Exception e1){
				ThrowableReporter.report(e);
			}
			ThrowableReporter.report(e);
		}
	}
	
}
